//
//  ViewController.m
//  SingleSelection
//
//  Created by whz on 17/4/18.
//  Copyright © 2017年 王鸿政_whz. All rights reserved.
//

#import "ViewController.h"
#import "WhzTableViewCell.h"

#define KScreenW [UIScreen mainScreen].bounds.size.width
#define KScreenH [UIScreen mainScreen].bounds.size.height
#define KCellID @"cellID"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *whzTableView;
@property(nonatomic, strong)NSMutableArray *dataArrayM;
@property(nonatomic, strong)NSIndexPath *indexPathChanged;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _dataArrayM = [self loadData];
    
    [self createUI];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.indexPathChanged == nil) {
    
        NSDictionary *dict = _dataArrayM[indexPath.row];
        NSMutableDictionary *dictM = dict.mutableCopy;
        [dictM removeObjectForKey:@"isSelect"];
        [dictM setObject:@"1" forKey:@"isSelect"];
        [_dataArrayM replaceObjectAtIndex:indexPath.row withObject:dictM.copy];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
        self.indexPathChanged = indexPath;
        
    } else {
        
        NSDictionary *dict = _dataArrayM[indexPath.row];
        NSMutableDictionary *dictM = dict.mutableCopy;
        [dictM removeObjectForKey:@"isSelect"];
        [dictM setObject:@"1" forKey:@"isSelect"];
        [_dataArrayM replaceObjectAtIndex:indexPath.row withObject:dictM.copy];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
        NSDictionary *dictDelete = _dataArrayM[self.indexPathChanged.row];
        NSMutableDictionary *dictDeleteM = dictDelete.mutableCopy;
        [dictDeleteM removeObjectForKey:@"isSelect"];
        [dictDeleteM setObject:@"0" forKey:@"isSelect"];
        [_dataArrayM replaceObjectAtIndex:self.indexPathChanged.row withObject:dictDeleteM.copy];
        [tableView reloadRowsAtIndexPaths:@[self.indexPathChanged] withRowAnimation:UITableViewRowAnimationNone];
        
        self.indexPathChanged = indexPath;
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArrayM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    WhzTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KCellID forIndexPath:indexPath];
    cell.dataDict = _dataArrayM[indexPath.row];
    
    return cell;
}

#pragma mark - method

- (void)createUI {

    [self.view addSubview:self.whzTableView];
    
    _whzTableView.delegate = self;
    _whzTableView.dataSource = self;
    
    [_whzTableView registerClass:[WhzTableViewCell class] forCellReuseIdentifier:KCellID];
}

- (NSMutableArray *)loadData {
    
    NSMutableArray *dataArrayM = [NSMutableArray arrayWithObjects:@{@"nameID":@"0",@"isSelect":@"0"},@{@"nameID":@"1",@"isSelect":@"0"},@{@"nameID":@"2",@"isSelect":@"0"},@{@"nameID":@"3",@"isSelect":@"0"},@{@"nameID":@"4",@"isSelect":@"0"},@{@"nameID":@"5",@"isSelect":@"0"},@{@"nameID":@"6",@"isSelect":@"0"},@{@"nameID":@"7",@"isSelect":@"0"},@{@"nameID":@"8",@"isSelect":@"0"},@{@"nameID":@"9",@"isSelect":@"0"},@{@"nameID":@"10",@"isSelect":@"0"},@{@"nameID":@"11",@"isSelect":@"0"},@{@"nameID":@"12",@"isSelect":@"0"},@{@"nameID":@"13",@"isSelect":@"0"},@{@"nameID":@"14",@"isSelect":@"0"},@{@"nameID":@"15",@"isSelect":@"0"}, nil];
    
    return dataArrayM;
}

#pragma mark - lazyload

- (UITableView *)whzTableView {

    if (!_whzTableView) {
        
        _whzTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KScreenW, KScreenH-64)];
    }
    return _whzTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
